<template>
    <section class="content">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
              <el-form-item>测点名称：
                <el-select v-model="queryPointlist" clearable placeholder="请选择" @change="selectKey">
                  <el-option v-for="item in queryPoint" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                时间：
                <el-date-picker
                  v-model="value2"
                  type="daterange"
                  align="right"
                  unlink-panels
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                  @change="timefirst"
                ></el-date-picker>
                数据类型：
                <el-select v-model="typeid" clearable placeholder="全部" class="select-w-100">
                  <el-option v-for="item in type" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-radio v-model="radio" label="1">周</el-radio>
                <el-radio v-model="radio" label="2">月</el-radio>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="inquire">查询</el-button>
              </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="14" class="bc-w">
            <div class="chartLine" id="chartLine"></div>
        </el-col>
    </section>
</template>
<script >
import echarts from "echarts";
export default {
  data() {
    return {
      queryPoint:[],
      queryPointlist:1,
      chartLine: null,
      type:[
        {id:1,name:"设备数据"},
        {id:2,name:"人工数据"}
      ],
      typeid:"",
      segments:[
        {id:1,name:"2"},
        {id:2,name:"5"},
        {id:3,name:"10"},
        {id:4,name:"20"},
        {id:5,name:"30"},
      ],
      segmentsid: 3,
      radio: '1',
      value2:[],
      series:[],
      xAxis:[],
      pickerOptions: {
        //仅当天前时间
        disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
        },
        shortcuts: this.shortcuts
      },
    };
  },
  methods: {
    timefirst(t){
      this.value2 = t
    },
    selectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.queryPoint.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    SegselectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.segments.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
     inquire(){
      if(this.queryPointlist != null && this.value2[0] != undefined && this.value2[1] != undefined){
        let data = {
          id: this.queryPointlist,
          beginDate: this.value2[0],
          endDate: this.value2[1],
          type: this.typeid,
          statisticType: 2,
          dateMode: this.radio
        }
        this.projectModel.collectDataStatistic(data).then(res => {
          
          this.xAxis = res.data.xAxis.data;
          this.series = res.data.series;
          this.drawCharts();
        }).catch(err => {
          console.log(err.message)
        })
      }
    },
    drawLineChart() {
      this.chartLine = echarts.init(document.getElementById("chartLine"));
      
      var _this = this
      var series = [];
      for (var i = 0; i < _this.series.length; i++) {
        series.push({
          name: _this.series[i].name,
          type: "bar",
          data: _this.series[i].value,
          markPoint: {
            data: [
              { type: "max", name: "最大值" },
              { type: "min", name: "最小值" }
            ]
          },
          markLine: {
            data: [{ type: "average", name: "平均值" }]
          }
        });
      }

      this.chartLine.setOption({
        title: {
          left: "center",
          text: _this.series[0].name+"时间分布图",
          subtext: _this.value2[0]+"至"+_this.value2[1]
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        toolbox: {
            orient: 'vertical',
            show: true,
            itemSize: 16,
            feature: {
              restore: {},
              saveAsImage: {}
            }
          },
        xAxis: [
          {
            name: '时间范围',
            type: "category",
            boundaryGap: true,
            data: _this.xAxis
          }
        ],
        yAxis: [
          { 
            name: '个数',
            type: "value"
          }
        ],
        series: series
      });
    },
    drawCharts() {
      this.drawLineChart();
    },
    date() {
            const end = new Date();
		        var seperator1 = "-";
		        var year = end.getFullYear();
		        var month = end.getMonth()+1;
		        var strDate = end.getDate();
		        if (month >= 1 && month <= 9) {
		            month = "0" + month;
		        }
		        if (strDate >= 0 && strDate <= 9) {
		            strDate = "0" + strDate;
            }
            var endDate= year + seperator1 + month + seperator1 + strDate;
            
            const start = new Date();
            var startYear = start.getFullYear()-5;
            var startMonth = start.getMonth()+1;
            var startDate = start.getDate();
            if (startMonth >= 1 && startMonth <= 9) {
		            startMonth = "0" + startMonth;
		        }
		        if (startDate >= 0 && startDate <= 9) {
		            startDate = "0" + startDate;
            }
            var startDat = startYear + seperator1 + startMonth + seperator1 + startDate;
            this.value2 = [startDat,endDate] 
    },
  },
  mounted() {
    this.projectId = this.$route.query.projectId;
    this.date();
    this.inquire();
    let data = {
      projectId: this.projectId,
      monitorItemId: "8",
      resultType: "1"
    };
    this.projectModel
      .queryPoint(data)
      .then(res => {
        ;
        this.queryPoint = res.data;
        if(this.queryPoint.length > 0) {
          this.queryPointlist = this.queryPoint[0].id;
          this.inquire();
        }
      })
      .catch(err => {
        console.log(err.message);
      });
  }
};
</script>


<style scoped>
.content {
  padding-top: 20px;
  background-color: #ffffff;
  min-height: 680px;
}
.bc-w {
  width: 100%;
  height: calc(100% - 90px);
  padding: 10px;
}
.chartLine {
  width: calc(100% - 30px);
  height: calc(100% - 30px);
  padding: 20px;
}
.select-w-100 {
  width: 100px;
}
</style>